package com.yuanthink.gwt.client.workplace;

import java.util.List;

import com.extjs.gxt.ui.client.Style.Scroll;
import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.data.BaseModelData;
import com.extjs.gxt.ui.client.data.BaseTreeLoader;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.data.TreeLoader;
import com.extjs.gxt.ui.client.data.TreeModel;
import com.extjs.gxt.ui.client.data.TreeModelReader;
import com.extjs.gxt.ui.client.event.SelectionChangedEvent;
import com.extjs.gxt.ui.client.event.SelectionChangedListener;
import com.extjs.gxt.ui.client.event.SelectionService;
import com.extjs.gxt.ui.client.event.SourceSelectionChangedListener;
import com.extjs.gxt.ui.client.store.TreeStore;
import com.extjs.gxt.ui.client.util.IconHelper;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.treepanel.TreePanel;
import com.yuanthink.gwt.client.eventbus.EventBus;
import com.yuanthink.gwt.client.registry.IWindowFactory;
/**
 * 
 * @author 06peng
 *
 */
public class ColumnTreePanel extends LayoutContainer implements IWindowFactory {

	EventBus eventBus;
	List<ModelData> actions;
	TreeStore<ModelData> treeStore;
	TreePanel<ModelData> tree;
	ColumnModel models;
	

	public EventBus getEventBus() {
		return eventBus;
	}

	public void setEventBus(EventBus eventBus) {
		this.eventBus = eventBus;
	}

	@Override
	public LayoutContainer createWindow(ModelData config) {
		ColumnTreePanel ctp = new ColumnTreePanel();
		ctp.init();
		return ctp;
	}
	
	public void bind() {
		SelectionService.get().addListener(
				new SelectionChangedListener<TreeModel>() {
					public void selectionChanged(
							SelectionChangedEvent<TreeModel> event) {
						ModelData m = event.getSelectedItem();
						if (null != m && m instanceof Entry) {
							String defaultAction = m.get("defaultAction");
							if (defaultAction != null && defaultAction.length() > 0) {
					            final BaseModelData eventData = new BaseModelData();
					            eventData.set("actionId", defaultAction);
					            eventBus.fireEvent("action", eventData);

					        } else {
					        	Entry entry = (Entry) m;
					            final BaseModelData eventData = new BaseModelData();
					            eventData.set("actionId", entry.get("actionId"));
					            eventBus.fireEvent("action", eventData);
					        }
						}
					}
				});
	}
	
	public void init() {
		this.setScrollMode(Scroll.NONE);
        setLayout(new FitLayout());
		TreeLoader<ModelData> loader = new BaseTreeLoader<ModelData>(
				new TreeModelReader<List<ModelData>>()) {
			@Override
			public boolean hasChildren(ModelData parent) {
				return parent instanceof Category;
			}

		};
		treeStore = new TreeStore<ModelData>(loader);
		models = new ColumnModel();
		tree = new TreePanel<ModelData>(treeStore);
		tree.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);
		tree.getStyle().setLeafIcon(IconHelper.createStyle("icon-list"));
		tree.setAutoLoad(true);
		tree.setDisplayProperty("name");
		this.add(tree);
		SelectionService.get().addListener(
				new SourceSelectionChangedListener(tree.getSelectionModel()));
		SelectionService.get().register(tree.getSelectionModel());

		loader.load(models);
		bind();
	}
	
	public TreePanel<ModelData> getTree() {
		return tree;
	}
}
